package com.zxy.furns.web;/**
 * ClassName: MemberServlet
 * Package: com.zxy.furns.web
 *
 * @Author zxy
 * @Create 2025/3/21 14:27
 * @Version 1.0
 * title:
 * Description:
 */


import com.google.code.kaptcha.servlet.KaptchaServlet;
import com.google.gson.Gson;
import com.zxy.furns.entity.Member;
import com.zxy.furns.service.MemberService;
import com.zxy.furns.service.impl.MemberServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/memberServlet")
public class MemberServlet extends BasicServlet {

    private MemberService memberService = new MemberServiceImpl();


    protected void isExistsUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean existsUsername = memberService.isExistsUsername(username);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("existsUsername",existsUsername);
        Gson gson = new Gson();
        String resultJson = gson.toJson(jsonMap);
        resp.getWriter().write(resultJson);
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet被调用了...");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Member member = memberService.login(new Member(null, username, password, null));
        if (member == null) {
            System.out.println("登录失败，不存在该用户");
            req.setAttribute("msg", "用户名或密码错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("member", member);
            System.out.println("登陆成功，该用户的用户名为：" + username + ";密码为：" + password);
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        }
    }


    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("RegisterServlet被调用了...");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        if (token != null && token.equalsIgnoreCase(code)) {
            if (!memberService.isExistsUsername(username)) {
                //如果没有被注册，那么我们们就可以进行注册
                System.out.println("用户名:" + username + "不存在，可以注册");
                Member member = new Member(null, username, password, email);
                if (memberService.register(member)) {
                    System.out.println("注册成功。详细信息为：" + member.getUsername() + ";" + member.getPassword() + ";" + member.getEmail());
                    req.getRequestDispatcher("/views/member/register_ok.jsp").forward(req, resp);
                } else {
                    System.out.println("注册失败");
                    req.getRequestDispatcher("/views/member/register_fail.jsp").forward(req, resp);

                }

            } else {
                System.out.println("该用户名" + username + "已经被注册！");
                req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);

            }
        } else {
            //这里是验证码错误的情况
            req.setAttribute("msg", "验证码错误");
            //我们先做一个数据回显。如果注册是时候验证码输入错误，那么显示用户名和邮箱在上面，方便用户重复输入
            req.setAttribute("username", username);
            req.setAttribute("email", email);
            //这里再来做一个处理：注册失败依旧停留在注册版块
            req.setAttribute("active", "register");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }


    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁当前用户的session，再重定向到首页
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
        System.out.println("req.getContextPath() = " + req.getContextPath());
    }
}